<template>
    <el-container>
        <el-container class="is-vertical">
            <div class="panel-search-box">
                <div class="panel-search">
                    <el-input v-model="search.name" placeholder="词汇量范围" clearable  class="w165"></el-input>
                    <el-select v-model="search.level" placeholder="级别" clearable filterable class="w165">
						<el-option :label="item.name" :value="item.value" v-for="(item,index) in levelData" :key="index"></el-option>
					</el-select>
                    <el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
                    <!-- <el-button type="primary" icon="el-icon-plus" @click="table_add" v-if="permission.add">增加</el-button> -->
                </div>
            </div>
            <el-main class="nopadding">
                <scTable ref="table" :apiObj="$API.vocabularyRule.list" remoteSort @dataChange="dataChange">
                    <el-table-column label="词汇量范围" prop="name" align="left"></el-table-column>
                    <el-table-column label="词汇最低数量" prop="minimumNumber" align="center" width="120"></el-table-column>
                    <el-table-column label="范围级别" prop="level" width="120" align="center">
                        <template #default="scope">
                            {{scope.row.level  ? scope.row.level.name : ''}}
                        </template>
                    </el-table-column>
                    <!-- <el-table-column label="词汇数量" prop="words" width="120" align="center">
                    </el-table-column> -->
                    <el-table-column label="排序" prop="sort" width="120" align="center">
                    </el-table-column>
                    <el-table-column label="创建时间" prop="createdAt" width="150" align="center"></el-table-column>
                    <el-table-column label="操作" fixed="right" align="center" width="220">
                        <template #default="scope">
                            <el-button type="text" size="small" @click="table_show(scope.row, scope.$index)">查看</el-button>
                            <el-divider v-if="permission.edit" direction="vertical"></el-divider>
                            <el-button type="text" size="small" @click="table_edit(scope.row, scope.$index)" v-if="permission.edit">编辑</el-button>
                            <el-divider direction="vertical"></el-divider>
                            <el-button type="text" size="small" @click="table_vocabulary(scope.row)">词汇管理</el-button>
                            <!-- <el-divider direction="vertical" v-if="permission.delete"></el-divider>
                            <el-dropdown v-if="permission.delete">
                                <el-button type="text" size="small">更多<i class="el-icon-arrow-down el-icon--right"></i></el-button>
                                <template #dropdown>
                                    <el-dropdown-menu>
                                        <el-popconfirm v-if="permission.delete" :title="'确认是否要删除规则：' + scope.row.name + '?'" @confirm="table_del(scope.row)">
											<template #reference>
												<el-dropdown-item v-if="permission.delete">删除</el-dropdown-item>
											</template>
										</el-popconfirm>
                                    </el-dropdown-menu>
                                </template>
                            </el-dropdown> -->
                        </template>
                    </el-table-column>
                </scTable>
            </el-main>
        </el-container>
    </el-container>

    <save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSuccess" :categoryData="categoryData" :levelData="levelData" :difficultyData="difficultyData" :bookData="bookData" @closed="dialog.save=false"></save-dialog>

    <detail-dialog v-if="dialog.detail" ref="detailDialog" @closed="dialog.detail=false"></detail-dialog>

    <vocabulary-dialog v-if="dialog.vocabulary" ref="vocabularyDialog" @success="handleSuccess" @closed="handVocabularyClose"></vocabulary-dialog>

</template>

<script>
import { rolePermission } from '@/utils/permission'
import saveDialog from './save'
import detailDialog from './detail'
import vocabularyDialog from './vocabulary'
export default {
    components: {
        saveDialog,
        detailDialog,
        vocabularyDialog
    },
    data() {
        return {
            dialog: {
                detail: false,
                save: false,
                vocabulary: false,
            },
            search: {
                name: null,
            },
            permission: {
				add: rolePermission('system-vocabulary-add'),
				edit: rolePermission('system-vocabulary-edit'),
				delete: rolePermission('system-vocabulary-delete')
			},
            levelData: [
                {'name': '初级', 'value': 1},
                {'name': '中级', 'value': 2},
                {'name': '高级', 'value': 3},
            ],
        }
    },
    mounted() {
        
    },
    methods: {
        dataChange(res, tableData) {
			let total = res.data.total
            let perPage = res.data.perPage
            let page = (this.$refs.table && this.$refs.table.currentPage) || 1
            tableData.forEach((item, index) => {
				item.index = total - (page * perPage + index - perPage)
            })
        },
        //查看
        table_show(row) {
            this.dialog.detail = true
			this.$nextTick(() => {
				this.$refs.detailDialog.open(row.id)
			})
        },
        //添加
        table_add() {
            this.dialog.save = true
            this.$nextTick(() => {
                this.$refs.saveDialog.open()
            })
        },
        //编辑
        table_edit(row) {
            this.dialog.save = true
            this.$nextTick(() => {
                this.$refs.saveDialog.open('edit').setData(row)
            })
        },
        //删除
        async table_del(row) {
            await this.$API.vocabularyRule.action.delete({ id: row.id }).then(() => {
                this.$refs.table.refresh()
                this.$notify.success("删除成功")
            });
        },
        //词库管理
        table_vocabulary(row) {
			this.dialog.vocabulary = true
			this.$nextTick(() => {
				this.$refs.vocabularyDialog.open(row)
			})
		},
        // 搜索
        upsearch() {
            this.$refs.table.upData(this.search)
        },
        //本地更新数据
        handleSuccess() {
            this.$refs.table.refresh()
        }
    }
}
</script>

<style>
.position {
    display: flex;
    align-items: center;
}
.position-item {
    margin-right: 5px;
}
.position-item:last-child {
    margin-right: 0;
}
</style>
